/*******************************************************************************
                                HKLWRITE.C

  Title:        Hkl write package for Speden.
  Author:       Hanna Szoke
  Date:         2/21/95 (original)
  Function:     This package contains functions that handle 
		writing of (hkl) factors 

		They include:

			writeFolist()	...	new for SpEden
			writeFclist()		 "   "    "

*******************************************************************************/

#include	"util.h"
#include	"symmetry.h"
#include	"dims.h"
#include	"sp.h"                 


void writeFolist(char *filename)          

	/* 9/20/05: We write ALL the reflections */

{
     FILE	*fpout ;
     int	n ;

     fpout = fopen(filename, "w") ;
     if (fpout == NULL)
     {
          sprintf(message, "Cannot open %s", filename);
          Fatal(message) ;
     }

     if (useSig)
          for (n = 0; n < Nfob; n++) 
	           fprintf(fpout, "INDEX   %f   %f   %f   FOBS   %g   SIGMA   %g\n", 
                   *(Hlist+n), *(Klist+n), *(Llist+n), *(Folist+n), *(Siglist+n)) ;
     else 
          for (n = 0; n < Nfob; n++) 
	           fprintf(fpout, "INDEX   %f   %f   %f   FOBS   %g\n", 
                   *(Hlist+n), *(Klist+n), *(Llist+n), *(Folist+n)) ;
     
     fclose(fpout) ;
}

void writefobs_chunk(char *filename, int N)          
{
     static	FILE	*fpout ;
     static 	int	FIRST = TRUE ;
     int	n ;

     if (N == 0)
          fclose(fpout) ;

     if (FIRST) {
         FIRST = FALSE ;
         fpout = fopen(filename, "w") ;
         if (fpout == NULL)
         {
              sprintf(message, "Cannot open %s", filename);
              Fatal(message) ;
         }
     }
     if (useSig)
          for (n = 0; n < N; n++) 
             fprintf(fpout, "INDEX   %f   %f   %f   FOBS   %g   SIGMA   %g\n", 
             *(Hlist+n), *(Klist+n), *(Llist+n), *(Folist+n), *(Siglist+n)) ;
     else 
          for (n = 0; n < N; n++) 
             fprintf(fpout, "INDEX   %f   %f   %f   FOBS   %g\n", 
             *(Hlist+n), *(Klist+n), *(Llist+n), *(Folist+n)) ;
     
}

void writeFclist(char *filename)          
{
     FILE	*fpout ;
     COMPLEX	thisR ;
     real	amplitude ;
     real	phase ;
     int	n ;

     fpout = fopen(filename, "w") ;
     if (fpout == NULL)
     {
          sprintf(message, "Cannot open %s", filename);
          Fatal(message) ;
     }
     
     for (n = 0; n < Nfc; n++) {

          thisR.re = *(Fc_relist + n) ;
          thisR.im = *(Fc_imlist + n) ;

	  getAmpPhase(&thisR, amp0, &amplitude, &phase) ;

          fprintf(fpout, "INDEX %10.4f %10.4f %10.4f FCALC   %g   %g\n", 
               *(Fc_Hlist+n), *(Fc_Klist+n), *(Fc_Llist+n), amplitude, phase) ;
     }

     fclose(fpout) ;
}

